﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdatePurchaseHandler : ICommandHandler<CreateOrUpdatePurchaseCommand>
    {
        private readonly IPurchaseRepository purchaseRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdatePurchaseHandler(IPurchaseRepository purchaseRepository, IUnitOfWork unitOfWork)
        {
            this.purchaseRepository = purchaseRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdatePurchaseCommand command)
        {
            var purchase = new Purchase
            {
                ID = command.ID,
                ApprovalDate=command.ApprovalDate,
                ApprovalUserID=command.ApprovalUserID,
                Attactment=command.Attactment,
                Code=command.Code,
                CompanyID=command.CompanyID,
                ContactAddress=command.ContactAddress,
                ContactID=command.ContactID,
                ContactName=command.ContactName,
                CreatedDate=command.CreatedDate,
                CreatedUserID=command.CreatedUserID,
                Discount=command.Discount,
                DueDate=command.DueDate,
                Note=command.Note,
                Status=command.Status,
                StockID=command.StockID,
                SubTotal=command.SubTotal,
                Tax=command.Tax,
                TotalMoney=command.TotalMoney,
                UseVAT=command.UseVAT
            };
            if (purchase.ID == 0)
                purchaseRepository.Add(purchase);
            else
                purchaseRepository.Update(purchase);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdatePurchaseCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdatePurchaseCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
